/*
 * Copyright (C) 2004-2006  Autodesk, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of version 2.1 of the GNU Lesser
 * General Public License as published by the Free Software Foundation.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
#include "stdafx.h"
#include <gdal.h>
#include <gdal_priv.h>
#include <FdoWcsBandRasterGdal.h>
#include <FdoWcsRasterCapabilities.h>
#include <FdoWcsRect.h>
#include <FdoWcsEnvelope.h>
#include <FdoWcsStreamReaderGdalByRow.h>
#include <FdoWcsImage.h>
#include <FdoWcsRasterPropertyDictionaryGdal.h>
#include <Geometry/EnvelopeImpl.h>

FdoWcsBandRasterGdal::FdoWcsBandRasterGdal(FdoPtr<FdoWcsImage>& image, FdoWcsRect& bounds)
			: m_bNull(false), m_image(image), m_palette(NULL)
{
    FdoPtr<FdoFgfGeometryFactory> geomfactory = FdoFgfGeometryFactory::GetInstance();
    FdoPtr<FdoEnvelopeImpl> envelope = FdoEnvelopeImpl::Create(bounds.m_minX, bounds.m_minY, bounds.m_maxX, bounds.m_maxY);
    FdoPtr<FdoIGeometry> iGeometery = geomfactory->CreateGeometry(envelope);
	m_bounds = geomfactory->GetFgf(iGeometery);
}

FdoWcsBandRasterGdal::~FdoWcsBandRasterGdal()
{
}

void FdoWcsBandRasterGdal::Dispose()
{
	delete this;
}

bool FdoWcsBandRasterGdal::IsNull ()
{
	return m_bNull;
}

void FdoWcsBandRasterGdal::SetNull ()
{
	m_bNull = true;
	m_bounds = NULL;
}

FdoDataValue* FdoWcsBandRasterGdal::GetNullPixelValue ()
{
	_validate();
	return m_image->GetNullPixelValue();
}

FdoByteArray* FdoWcsBandRasterGdal::GetBounds ()
{
	_validate();
	return FDO_SAFE_ADDREF(m_bounds.p);
}

void FdoWcsBandRasterGdal::SetBounds (FdoByteArray* bounds)
{
	if (bounds == NULL)
        throw FdoException::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_1_INVALID_INPUT_ON_CLASS_FUNCTION),
                                                               L"FdoWcsBandRasterGdal::SetBounds",
                                                               L"bounds"));

	_validate();
	m_bounds = FDO_SAFE_ADDREF(bounds);
}

FdoRasterDataModel* FdoWcsBandRasterGdal::GetDataModel ()
{
	_validate();
	return m_image->GetDataModel();
}

void FdoWcsBandRasterGdal::SetDataModel (FdoRasterDataModel* dataModel)
{
	if (dataModel == NULL)
        throw FdoException::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_1_INVALID_INPUT_ON_CLASS_FUNCTION),
                                                               L"FdoWcsBandRasterGdal::SetDataModel",
                                                               L"dataModel"));

	_validate();
	FdoPtr<FdoWcsRasterCapabilities> rasterCap = new FdoWcsRasterCapabilities();
	if (!rasterCap->SupportsDataModel(dataModel))
		throw FdoCommandException::Create(NlsMsgGet(FDOWCS_11002_DATAMODEL_NOT_SUPPORTED, "Raster data model not supported."));

	m_image->SetDataModel(dataModel);
}

FdoInt32 FdoWcsBandRasterGdal::GetImageXSize ()
{
	_validate();
	return m_image->GetImageXSize();
}

void FdoWcsBandRasterGdal::SetImageXSize (FdoInt32 size)
{
    if (size <= 0)
        throw FdoException::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_1_INVALID_INPUT_ON_CLASS_FUNCTION),
                                                               L"FdoWcsBandRasterGdal::SetImageXSize",
                                                               L"size"));

	_validate();
	m_image->SetImageXSize(size);
}

FdoInt32 FdoWcsBandRasterGdal::GetImageYSize ()
{
	_validate();
	return m_image->GetImageYSize();
}

void FdoWcsBandRasterGdal::SetImageYSize (FdoInt32 size)
{
    if (size <= 0)
        throw FdoException::Create(FdoException::NLSGetMessage(FDO_NLSID(FDO_1_INVALID_INPUT_ON_CLASS_FUNCTION),
                                                               L"FdoWcsBandRasterGdal::SetImageYSize",
                                                               L"size"));

	_validate();
	m_image->SetImageYSize(size);
}

/// <returns>The dictionary of auxilliary raster properties.</returns> 
FdoIRasterPropertyDictionary* FdoWcsBandRasterGdal::GetAuxiliaryProperties ()
{
	return new FdoWcsRasterPropertyDictionayGdal(this);
}

void FdoWcsBandRasterGdal::SetStreamReader (FdoIStreamReader* reader)
{
	_validate();
}

FdoIStreamReader* FdoWcsBandRasterGdal::GetStreamReader ()
{
	_validate();
	return new FdoWcsStreamReaderGdalByRow(m_image);
}

RgbColor*	FdoWcsBandRasterGdal::GetPalette()
{
	_validate();
	return m_image->GetPalette();
}

FdoInt32 FdoWcsBandRasterGdal::GetNumberOfPaletteEntries()
{
	_validate();
	return m_image->GetNumberOfPaletteEntries();
}

// --------------------------------------------------------------------------
// Internally-used helper functions
// --------------------------------------------------------------------------
// Validate the raster is not null before any data retrieval operations
void FdoWcsBandRasterGdal::_validate()
{
	if (m_bNull || m_image == NULL)
		throw FdoCommandException::Create(NlsMsgGet(FDOWCS_11001_RASTER_NULL, "Operation failed because the raster is null."));
}


FdoWcsBandRasterGdalCollection* FdoWcsBandRasterGdalCollection::Create()
{
	return new FdoWcsBandRasterGdalCollection();
}

void FdoWcsBandRasterGdalCollection::Dispose()
{
	delete this;
}
